package org.germinus.telcoblocks.servicios.diagram.navigator;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserService;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.AnunciosEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.AnunciosNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.ClickToDialEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.ClickToDialNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.DesvioEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.DesvioNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.EnlaceComentarioEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.EnlaceEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.FacturacionEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.FacturacionNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.IMEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.IMNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.LlamadasEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.LlamadasNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.PersonalizacionEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.PersonalizacionNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.SERVICIOSEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.ServicioGenEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.ServicioGenNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.TelefonoEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.TelefonoNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.WebEditPart;
import org.germinus.telcoblocks.servicios.diagram.edit.parts.WebNombreEditPart;
import org.germinus.telcoblocks.servicios.diagram.part.TelcoblocksDiagramEditorPlugin;
import org.germinus.telcoblocks.servicios.diagram.part.TelcoblocksServiciosDiagramEditorPlugin;
import org.germinus.telcoblocks.servicios.diagram.part.TelcoblocksVisualIDRegistry;
import org.germinus.telcoblocks.servicios.diagram.providers.TelcoblocksElementTypes;
import org.germinus.telcoblocks.servicios.diagram.providers.TelcoblocksParserProvider;

/**
 * @generated
 */
public class TelcoblocksNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		TelcoblocksServiciosDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put(
						"Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		TelcoblocksServiciosDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put(
						"Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof TelcoblocksNavigatorItem
				&& !isOwnView(((TelcoblocksNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof TelcoblocksNavigatorGroup) {
			TelcoblocksNavigatorGroup group = (TelcoblocksNavigatorGroup) element;
			return TelcoblocksServiciosDiagramEditorPlugin.getInstance()
					.getBundledImage(group.getIcon());
		}

		if (element instanceof TelcoblocksNavigatorItem) {
			TelcoblocksNavigatorItem navigatorItem = (TelcoblocksNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (TelcoblocksVisualIDRegistry.getVisualID(view)) {
		case SERVICIOSEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://telcoblocks.germinus.com?SERVICIOS", TelcoblocksElementTypes.SERVICIOS_1000); //$NON-NLS-1$
		case ClickToDialEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://telcoblocks.germinus.com?ClickToDial", TelcoblocksElementTypes.ClickToDial_2001); //$NON-NLS-1$
		case LlamadasEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://telcoblocks.germinus.com?Llamadas", TelcoblocksElementTypes.Llamadas_2002); //$NON-NLS-1$
		case ServicioGenEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://telcoblocks.germinus.com?ServicioGen", TelcoblocksElementTypes.ServicioGen_2003); //$NON-NLS-1$
		case PersonalizacionEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://telcoblocks.germinus.com?Personalizacion", TelcoblocksElementTypes.Personalizacion_2004); //$NON-NLS-1$
		case IMEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://telcoblocks.germinus.com?IM", TelcoblocksElementTypes.IM_2005); //$NON-NLS-1$
		case FacturacionEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://telcoblocks.germinus.com?Facturacion", TelcoblocksElementTypes.Facturacion_2006); //$NON-NLS-1$
		case WebEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://telcoblocks.germinus.com?Web", TelcoblocksElementTypes.Web_2007); //$NON-NLS-1$
		case DesvioEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://telcoblocks.germinus.com?Desvio", TelcoblocksElementTypes.Desvio_2008); //$NON-NLS-1$
		case AnunciosEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://telcoblocks.germinus.com?Anuncios", TelcoblocksElementTypes.Anuncios_2009); //$NON-NLS-1$
		case TelefonoEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://telcoblocks.germinus.com?Telefono", TelcoblocksElementTypes.Telefono_2010); //$NON-NLS-1$
		case EnlaceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://telcoblocks.germinus.com?Enlace", TelcoblocksElementTypes.Enlace_4001); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = TelcoblocksServiciosDiagramEditorPlugin
				.getInstance().getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& TelcoblocksElementTypes.isKnownElementType(elementType)) {
			image = TelcoblocksElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof TelcoblocksNavigatorGroup) {
			TelcoblocksNavigatorGroup group = (TelcoblocksNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof TelcoblocksNavigatorItem) {
			TelcoblocksNavigatorItem navigatorItem = (TelcoblocksNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (TelcoblocksVisualIDRegistry.getVisualID(view)) {
		case SERVICIOSEditPart.VISUAL_ID:
			return getSERVICIOS_1000Text(view);
		case ClickToDialEditPart.VISUAL_ID:
			return getClickToDial_2001Text(view);
		case LlamadasEditPart.VISUAL_ID:
			return getLlamadas_2002Text(view);
		case ServicioGenEditPart.VISUAL_ID:
			return getServicioGen_2003Text(view);
		case PersonalizacionEditPart.VISUAL_ID:
			return getPersonalizacion_2004Text(view);
		case IMEditPart.VISUAL_ID:
			return getIM_2005Text(view);
		case FacturacionEditPart.VISUAL_ID:
			return getFacturacion_2006Text(view);
		case WebEditPart.VISUAL_ID:
			return getWeb_2007Text(view);
		case DesvioEditPart.VISUAL_ID:
			return getDesvio_2008Text(view);
		case AnunciosEditPart.VISUAL_ID:
			return getAnuncios_2009Text(view);
		case TelefonoEditPart.VISUAL_ID:
			return getTelefono_2010Text(view);
		case EnlaceEditPart.VISUAL_ID:
			return getEnlace_4001Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getSERVICIOS_1000Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getClickToDial_2001Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.ClickToDial_2001,
				(view.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry
						.getType(ClickToDialNombreEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getLlamadas_2002Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.Llamadas_2002,
				(view.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry
						.getType(LlamadasNombreEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getServicioGen_2003Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.ServicioGen_2003,
				(view.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry
						.getType(ServicioGenNombreEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getPersonalizacion_2004Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.Personalizacion_2004, (view
						.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry
						.getType(PersonalizacionNombreEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getIM_2005Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.IM_2005,
				(view.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry.getType(IMNombreEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getFacturacion_2006Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.Facturacion_2006,
				(view.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry
						.getType(FacturacionNombreEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5006); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getWeb_2007Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.Web_2007,
				(view.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry
						.getType(WebNombreEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5007); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getDesvio_2008Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.Desvio_2008,
				(view.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry
						.getType(DesvioNombreEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5008); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getAnuncios_2009Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.Anuncios_2009,
				(view.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry
						.getType(AnunciosNombreEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5009); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getTelefono_2010Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.Telefono_2010,
				(view.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry
						.getType(TelefonoNombreEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5010); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getEnlace_4001Text(View view) {
		IAdaptable hintAdapter = new TelcoblocksParserProvider.HintAdapter(
				TelcoblocksElementTypes.Enlace_4001,
				(view.getElement() != null ? view.getElement() : view),
				TelcoblocksVisualIDRegistry
						.getType(EnlaceComentarioEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			TelcoblocksServiciosDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 6001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return SERVICIOSEditPart.MODEL_ID.equals(TelcoblocksVisualIDRegistry
				.getModelID(view));
	}

}
